// pages/toDolist/toDolist.js
Page({
  data: {
    value:[
      {name:"zz",state:false},
      {name:"cc",state:true},
    ]
  },
  onLoad: function (options) {
    var value = wx.getStorageSync('value');
    if(value){
      this.setData({
        value
      })
    }else{
      wx.setStorageSync('value', [])
    }
  },
  // 搜索框
  onSearch(event){
    let detail = event.detail.trim()
    let value = this.data.value;
    if(value.every(res=>res.name!=detail)&&detail){
      value.push({
        name:detail,
        state:false
      })
      wx.setStorageSync('value', value)
      this.setData({
        value
      })
    }else{
      wx.showToast({
        title: '数据已存在',
        icon: ''
      })
    }
  },

  // 删除按钮
  onRemove(event){
    let index = event.currentTarget.dataset.id
    let value = this.data.value;
    value.splice(index,1)
    wx.setStorageSync('value', value)
    this.setData({
      value
    })
  },

  // 复选框
  onChange(event){
    let value = this.data.value;
    value.forEach((res,index)=>{
      if(index==event.currentTarget.dataset.id){
        res.state = event.detail
      }
    })
    wx.setStorageSync('value', value)
    this.setData({
      value
    })
  }
})